package com.ciwong.epaper.modules.me.ui;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.adapter.DubVideoRecordListAdapter;
import com.ciwong.epaper.modules.epaper.bean.VideoRecord;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.i.BaseQuickOnItemClickListener;
import com.ciwong.mobilelib.ui.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 趣味配音记录列表
 */

public class DubVideoRecordListActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {
    private RecyclerView mRvBookDesk;
    private DubVideoRecordListAdapter mDubVideoRecordListAdapter;
    private List<VideoRecord> mVideoRecordList = new ArrayList<>();
    private SwipeToLoadLayout mSwipeToLoadLayout;
    private int mPageIndex = 1;
    private int mPageSize = 10;
    private String mBookmarkTitle;
    private boolean mIsRefesh;
    private LinearLayout list_nodata;
    private ViewGroup listviewgroup;
    private Button btn_retry;

    @Override
    protected int setView() {
        return R.layout.fragment_book_desk;
    }

    @Override
    protected void findViews() {
        mRvBookDesk = findViewById(R.id.swipe_target);
        mSwipeToLoadLayout = findViewById(R.id.swipe_toLoad_layout);
        list_nodata = findViewById(R.id.ll_no_net);
        listviewgroup = findViewById(R.id.listviewgroup);
        btn_retry = findViewById(R.id.btn_retry);
    }

    @Override
    protected void initEvent() {
        setTitleText("配音记录");
        mSwipeToLoadLayout.setOnRefreshListener(this);
        mSwipeToLoadLayout.setOnLoadMoreListener(this);
        btn_retry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadData();
            }
        });
    }

    @Override
    protected void init() {
        mRvBookDesk.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mDubVideoRecordListAdapter = new DubVideoRecordListAdapter(R.layout.item_view_dub_video_record, mVideoRecordList);
        mRvBookDesk.setAdapter(mDubVideoRecordListAdapter);

        mDubVideoRecordListAdapter.setOnItemClickListener(new BaseQuickOnItemClickListener() {
            @Override
            public void avertRepeatOnClick(BaseQuickAdapter adapter, View view, int position) {
                try {
                    VideoRecord videoRecord = mVideoRecordList.get(position);
                    if (null != videoRecord) {
                        //点击更多配音记录详情
                        MeJumpManager.jumpToDubVideoRecordDetailH5Activity(DubVideoRecordListActivity.this, R.string.go_back, "", videoRecord.getDoWorkId());
                    }
                } catch (Exception e) {

                }

            }
        });
    }

    @Override
    protected void loadData() {
        mIsRefesh = true;
        mPageIndex = 1;
        if (!NetworkUtils.isOnline()) {
            list_nodata.setVisibility(View.VISIBLE);
            listviewgroup.setVisibility(View.GONE);
        } else {
            list_nodata.setVisibility(View.GONE);
            listviewgroup.setVisibility(View.VISIBLE);
            getBookBooksFromNet(mPageIndex);
        }

    }

    public void getBookBooksFromNet(int pageIndex) {
        EpaperDao.getInstance().getDubbingVideoRecordList(pageIndex, mPageSize, new BaseExtCallBack(DubVideoRecordListActivity.this) {
            @Override
            public void failed(int errorCode, Object data) {
                super.failed(errorCode, data);
                mSwipeToLoadLayout.setRefreshing(false);
                mSwipeToLoadLayout.setLoadingMore(false);
            }

            @Override
            public void failed(Object data) {
                super.failed(data);
                mSwipeToLoadLayout.setRefreshing(false);
                mSwipeToLoadLayout.setLoadingMore(false);
            }

            @Override
            public void success(Object data) {
                mSwipeToLoadLayout.setRefreshing(false);
                mSwipeToLoadLayout.setLoadingMore(false);
                try {
                    if (data != null && data instanceof ArrayList) {
                        List<VideoRecord> videoRecordList = (ArrayList<VideoRecord>) data;
                        if (mIsRefesh) {
                            if (videoRecordList != null && videoRecordList.size() >= 0) {
                                mVideoRecordList = videoRecordList;
                                mDubVideoRecordListAdapter.setNewData(mVideoRecordList);
                            }
                        } else {
                            if (videoRecordList != null && videoRecordList.size() >= 0) {
                                mVideoRecordList.addAll(videoRecordList);
                                mDubVideoRecordListAdapter.addData(videoRecordList);
                            }
                        }

                    }
                } catch (Exception e) {
                    e.getStackTrace();
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        mIsRefesh = true;
        mPageIndex = 1;
        getBookBooksFromNet(mPageIndex);
    }

    @Override
    public void onLoadMore() {
        mIsRefesh = false;
        mPageIndex++;
        getBookBooksFromNet(mPageIndex);
    }
}

